<?php
/*
 * Created on 26.01.2006
 *
 */
 
m2import('phpinchains/util/ValidatorHelper.php');
m2import('phpinchains/util/ArrayWalker.php');

 
class phpinchains_validators_UploadValidator
{

    /**
     * Upload file is of one of the given file types
     *
     * params: type; e.g.  type:image/jpeg,image/gif
     */
    function fileType ($env, $actionform, $fieldname, $validationParam)
    {
        $params = phpinchains_util_ValidatorHelper::parseValidationParam($validationParam, "fileType", array("type"));
        if (is_array($actionform->get($fieldname)))
        {
            $file = $actionform->get($fieldname);
            if (isset($file['tmp_name']) && isset($file['name']) && isset($file['type']) && !in_array($file['type'], ArrayWalker::trim(split(',', $params['type'])))
                && isset($file['size']) && $file['size'] > 0)
            {
                $errors = & $actionform->getRef('validationErrors');
                $errors->addField($fieldname);
                return true;
            }
        }
    }

    /**
     * Upload file has one of the given file extensions
     *
     * params: extension; e.g. extension:csv,txt
     */
    function fileExtension ($env, $actionform, $fieldname, $validationParam)
    {
        $params = phpinchains_util_ValidatorHelper::parseValidationParam($validationParam, "fileExtension", array("extension"));
        if (is_array($actionform->get($fieldname)))
        {
            $file = $actionform->get($fieldname);
            preg_match('/\.(\w+)$/',$file['name'],$matches);
            $extension = strtolower($matches[1]);
            if (isset($file['tmp_name']) && isset($file['name']) && isset($file['type']) && !in_array($extension, ArrayWalker::trim(split(',', $params['extension'])))
                && isset($file['size']) && $file['size'] > 0)
            {
                $errors = & $actionform->getRef('validationErrors');
                $errors->addField($fieldname);
                return true;
            }
        }
    }

    /**
     * Upload file is of one of the given file types
     * 
     * params: min, max; format: 1 = 1b = 1 byte, 1k = 1 kilobyte, 1m = 1 megabyte;
     */
    function fileSize ($env, $actionform, $fieldname, $validationParam)
    {
        $params = phpinchains_util_ValidatorHelper::parseValidationParam($validationParam, "fileSize", array("max"));
        preg_match('/(\d+)(b|k|m)?$/', $params['max'], $matches);
        $max = (int) $matches[1] * ($matches[2] == 'm'?(1024*1024):($matches[2] == 'k'?1024:(int) $matches[1]));
        if (empty($params['min'])) {
            $min = 0;
        } else {
            preg_match('/(\d+)(b|k|m)?$/', $params['min'], $matches);
            $min = (int) $matches[1] * ($matches[2] == 'm'?(1024*1024):($matches[2] == 'k'?1024:(int) $matches[1]));
        }
        if (is_array($actionform->get($fieldname)))
        {
            $file = $actionform->get($fieldname);
            if (isset($file['tmp_name']) && isset($file['name']) && isset($file['type']) && isset($file['size']) 
                && $file['size'] > 0 && ($file['size'] < $min || $file['size'] > $max))
            {
                $errors = & $actionform->getRef('validationErrors');
                $errors->addField($fieldname);
                return true;
            }
        }
    }
    
}

?>